#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdlib.h>


void DoSomething()
{
    printf("DoSomething\n");
}

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        int* p = NULL;
        printf("I am child process\n");
        sleep(1);
        //*p = 0;
        printf("child exit\n");
        exit(0);
    }
    else if(id < 0)
    {
        perror("fork falied\n");
        exit(1);
    }
    else
    {
        int status = 0;
        while(1)
        {
            pid_t fid = waitpid(id,&status,WNOHANG);
            DoSomething();
            if(fid != 0)
            {
                printf("wait success\n");
                break;
            }
        }
        //分析退出状态
        if(WIFEXITED(status))
            //正常退出
        {
            printf("正常退出：status:%d|exit code:%d\n",status,(status>>8)&0xff);
        }
        else
            //异常退出
        {
            
            printf("异常退出：status:%d|exit signal:%d\n",status,(status&0x7f));
        }
    }
    return 0;
}
